﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;
using XQ.WeChatOfficialAccount.BLL.Sys;
using XQ.WeChatOfficialAccount.DAL;
using XQ.WeChatOfficialAccount.DAL.Model;
using XQ.WeChatOfficialAccount.DAL.Model.Sys;
using XQ.WeChatOfficialAccount.WebAPI.Filter;

namespace XQ.WeChatOfficialAccount.WebAPI.Controllers.System
{
    /// <summary>
    /// 角色页面权限相关
    /// </summary>
    //[FormAuthenticationFilter]
    public class RightController : ApiController
    {
        private readonly RightMangerBll _rightManager = new RightMangerBll();

        /// <summary>
        /// 检查菜单角色
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="menuId">菜单ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/Right/GetMenuButtons")]
        public IEnumerable<RoleMenuModel> GetMenuButtons(int roleId, int menuId)
        {
            return _rightManager.GetMenuButtons(roleId, menuId);
        }

        /// <summary>
        /// 根据角色ID获取所有对应的菜单权限
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/Right/GetMenuButtonsByRoleID")]
        public SaveMenusButtonsModel GetMenuButtonsByRoleID(int roleId)
        {
            return _rightManager.GetMenuButtonsByRoleID(roleId);
        }

        /// <summary>
        /// 判断角色与页面权限
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="menuId">页面Id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/Right/CheckRightInfo")]
        public bool CheckRightInfo(int roleId, int menuId)
        {
            return _rightManager.CheckRoleMenu(roleId, menuId);          
        }
        /// <summary>
        /// 获取角色页面Buttons
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/Right/GetRoleMenuButtons")]
        public IEnumerable<CheckInfo> GetRoleMenuButtons(string ids)
        {
            var rightManager = new RightMangerBll();
            string[] _ids = ids.Split('%');
            var data = new List<CheckInfo>();

            for (int i = 0; i < _ids.Length; i++)
            {
                var item = new CheckInfo();
                item.ControlId = _ids[i].Split('$')[0];
                string menuIdAndRoleIdAndBtnIds = _ids[i].Split('$')[1];
                item.Result = rightManager.GetRoleButton(Convert.ToInt32(menuIdAndRoleIdAndBtnIds.Split(',')[0]),
                     Convert.ToInt32(menuIdAndRoleIdAndBtnIds.Split(',')[1]),
                     Convert.ToInt32(menuIdAndRoleIdAndBtnIds.Split(',')[2]));
                data.Add(item);
            }

            return data;
        }

        /// <summary>
        ///保存角色权限分配
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="menuId"></param>
        /// <param name="btnId"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/Right/UpdateRoleMenuButton")]
        public bool UpdateRoleMenuButton(int roleId, int menuId, int btnId, bool flag)
        {
            var rightManager = new RightMangerBll();
            rightManager.SaveRights(roleId, menuId, btnId, flag);

            return true;
        }

        /// <summary>
        /// 角色分配权限
        /// </summary>
        /// <param name="saveMenusButtonsModel">保存参数模型</param>
        /// <returns></returns>
        [HttpPost]
        [Route("Api/Right/SaveMenusButtons")]
        public bool SaveMenusButtons(SaveMenusButtonsModel saveMenusButtonsModel)
        {
            var rightManager = new RightMangerBll();
            return rightManager.SaveMenusButtons(saveMenusButtonsModel.RoleId, saveMenusButtonsModel.MenuIDs, saveMenusButtonsModel.HalfMenuIDs);
        }
    }

    /// <summary>
    /// checkbox 状态及ID
    /// </summary>
    public class CheckInfo
    {
        public string ControlId { get; set; }
        public bool Result { get; set; }
    }
}